package com.colorlight.cloud.complete.entity.instructions;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.complete.api.CompleteEntityScheduleActions;
import com.colorlight.cloud.complete.entity.po.PresetInfo;
import com.colorlight.iot.proto.complete.instruction.CompleteInstruction;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 发送设备保存预置
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class SavePreset implements CompleteEntityScheduleActions {

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class InputMsg implements Serializable {
        /**
         * 发送器序号
         */
        private Integer prcIndex;

        /**
         * 屏幕分组数
         */
        private Integer groupIndex;

        /**
         * 预置参数序号
         */
        private Integer presetIndex;

        /**
         * 预置参数名称
         */
        private String presetName;

        /**
         * 是否预置颜色亮度相关参数
         */
        private Boolean bSaveColorBrightness;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class OutputMsg implements Serializable {
        /**
         * 预置信息
         */
        private List<PresetInfo> presetInfo;
    }

    private InputMsg inputMsg;
    private OutputMsg outputMsg;

    @Override
    public ByteString objectConvertBytes() {
        final CompleteInstruction.SavePreset.InputMsg.Builder inputMsgBuilder =
                CompleteInstruction.SavePreset.InputMsg.newBuilder();
        SavePreset.InputMsg thisInput = this.getInputMsg();
        inputMsgBuilder.setPrcIndex(thisInput.getPrcIndex());
        inputMsgBuilder.setGroupIndex(thisInput.getGroupIndex());
        inputMsgBuilder.setPresetIndex(thisInput.getPresetIndex());
        inputMsgBuilder.setPresetName(thisInput.getPresetName());
        inputMsgBuilder.setBSaveColorBrightness(thisInput.getBSaveColorBrightness());

        final CompleteInstruction.SavePreset.Builder builder =
                CompleteInstruction.SavePreset.newBuilder();
        builder.setInput(inputMsgBuilder);
        return builder.build().toByteString();
    }

    @Override
    public SavePreset byteConvertObject(ByteString bytes) {
        CompleteInstruction.SavePreset savePreset = null;
        try {
            savePreset = CompleteInstruction.SavePreset.parseFrom(bytes);
        } catch (InvalidProtocolBufferException e) {
            log.error("", e);
        }
        if (Objects.isNull(savePreset)) {
            throw new ParameterException(ExceptionEnum.MSG_PARAMETER_EXCEPTION);
        }
        SavePreset.OutputMsg outputMsg = new SavePreset.OutputMsg();
        CompleteInstruction.SavePreset.OutputMsg output = savePreset.getOutput();
        List<PresetInfo> collect = output.getPresetInfoList().stream().map(e -> new PresetInfo().formProto(e)).collect(Collectors.toList());
        outputMsg.setPresetInfo(collect);
        this.setOutputMsg(outputMsg);
        return this;
    }
}

